package com.xf.zk.curator.example.znode;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class DeleteData {
    public static void main(String[] args) throws Exception {
        String path = "/zk-curator";
        CuratorFramework client = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183").sessionTimeoutMs(5000)
                        .retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();
        client.start();
        String path1 = path + "/c1";
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path1, "init".getBytes());
        String path2 = path + "/c2";
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path2, "init".getBytes());
        Stat stat = new Stat();
        // 查看path 下的子节点
        System.out.println(client.getChildren().forPath(path));
        // delete node and delete all children
        client.delete().deletingChildrenIfNeeded().withVersion(stat.getVersion()).forPath(path);
        System.out.println("success delete znode " + path);
        client.close();
        Thread.sleep(Integer.MAX_VALUE);
    }
}
